package com.noble.meether.chat.ui.agora.act.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.invoke.Invoker;
import com.noble.glamour.module.logic.invoke.WebAppInterface;
import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * 通用的网络请求处理panel
 * Created by ZRP on 2016/12/8.
 */
public class RtcWebPanel {

    private String url;
    private WebView webView;
    private WebSettings webSettings;
    private View contentView;

    public RtcWebPanel(Context context, String url) {
        this.url = url;
        initView();
    }

    private void initWebView() {
        clearReference();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        webView = new WebView(App.context);
        webView.setLayoutParams(params);
        this.contentView = webView;
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initView() {
        initWebView();

        webSettings = webView.getSettings();
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setAllowFileAccess(false);//设置允许访问文件数据
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不从缓存加载，确保每次进入的时候都是从服务器请求的最新页面
//        if (PerformanceHelper.isHighPerformance(getContext())) {
            /*为高内存手机 增加清晰度*/
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
//        }

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                webView.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
                super.onReceivedError(webView, errorCode, description, failingUrl);
//                customFrameLayout.showOfIndex(FRAME_ERROR);
//                if (webListener != null) webListener.onLoadFinish(WebLoadStatus.ERROR);
            }

            @Override
            public void onPageFinished(WebView webView, String url) {
//                if (isLoadingInnerControl) hideLoading();
//                if (webListener != null) webListener.onLoadFinish(WebLoadStatus.FINISH);
//                if ("about:blank".equals(url) && webView.getTag() != null) {
//                    webView.loadUrl(webView.getTag().toString());
//                } else {
//                    webView.setTag(url);
//                }
            }

            @Override
            public void onPageStarted(WebView webView, String url, Bitmap bitmap) {
                super.onPageStarted(webView, url, bitmap);
//                customFrameLayout.showOfIndex(FRAME_LOADING);
            }

            @Override
            public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
                super.onReceivedSslError(webView, sslErrorHandler, sslError);
                sslErrorHandler.proceed();
            }
        });
        webView.addJavascriptInterface(new WebAppInterface(App.context, webView), "Android");
        webView.requestFocus();
//        webView.setTag(url);
//        webView.loadUrl("about:blank");
        webView.loadUrl(url);
        webView.setBackgroundColor(0);
    }

    /**
     * 重新加载新的url地址
     *
     * @param url 新的url地址
     */
    public void resetUrl(String url) {
        this.url = url;
        if (webView != null) {
            webView.loadUrl(url);
            webView.setBackgroundColor(0);
        }
    }

    /**
     * @return 获取展示的WebView实例
     */
    public WebView getWebView() {
        return webView;
    }

    public View getContentView() {
        return contentView;
    }

    /**
     * 清除webView引用并置空
     */
    public void clearReference() {
        if (webView != null) {
//            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            Invoker.getInstance().setWebView(null);
            webView.loadUrl("about:blank");
            webView.stopLoading();
            webView.setWebChromeClient(null);
            webView.setWebViewClient(null);
            webSettings.setJavaScriptEnabled(false);
            webView.clearHistory();
            webView.stopLoading();
            webView.clearCache(true);
            webView.removeJavascriptInterface("Android");
            webView.destroy();
            webView = null;
        }
    }
}
